<?php
class userController extends appController {    

    public function detail($id){
        if(empty($id)){
            return $this->unknownAction();
        }
        
        $data = $this->_model->read($id);
        $this->view->set('item',$data[0]);
        return $this->view();       
    }
    
    public function create($username=null, $password=null, $email=null, $role=null, $is_active=1, $confirmed=1){
        if($_SERVER["REQUEST_METHOD"]=='POST'){     
            $id = $this->_model->create($username,$password,$email,$role,$is_active,$confirmed);
            if($id !== false){
                header('Location: blog/detail/'.$id);
                exit;
            }
        }
        return $this->view();
    }
    //the function are used for check the username exist form database
    public function chk_user_exist($username) {
        $query = "Select * from users where username = '$username'";
        $result = mysql_query($query) or die("result");
        $row = mysql_num_rows($result);
        if ($row > 0) {

            return FALSE;
        } else {

            return TRUE;
        }
    }
     //create fuction for check username and password 
    public function login($username = null, $password = null) {
        
        if($_SERVER["REQUEST_METHOD"]=='POST'){     
            $id = $this->_model->create($username,$password,$email,$role,$is_active,$confirmed);
            if($id !== false){
                header('Location: blog/detail/'.$id);
                exit;
            }
        }
        
        return $this->view();
        
    }

    // the function are used for insert data in userprofile tabel
    public function insertUserPrfdb($Fname, $Lname, $Mname, $Phone, $Country, $State, $City, $Discrp, $Role, $Active, $Confirm) {
        $sql = "SELECT *  FROM users WHERE id='" . $_SESSION['user_login_id'] . "' and username='" . $_SESSION['user_login_name'] . "'";
        $result = mysql_query($sql);
        $row = mysql_fetch_array($result);
        if ($row['role'] == "") {
            $sql_insert = "INSERT INTO userprofile (user_id, first_name, last_name, middel_name, phone, country, state, city, descriptions ) "
                    . "VALUES ('" . $_SESSION['user_login_id'] . "', '$Fname', '$Lname', '$Mname', '$Phone', '$Country', '$State', '$City', '$Discrp')";
            mysql_query($sql_insert) or die(mysql_errno());
            $sql_update = "update users set role='" . $Role . "',is_active='" . $Active . "',confirmed='" . $Confirm . "' where id='" . $_SESSION['user_login_id'] . "' ";
            mysql_query($sql_update) or die("update");
        } else {
            echo "eror";
        }
    }

    public function CheckUserProfiles($userId) {
        
        $query = "select * from userprofile where id='" . $userId . "'";
        $set = mysql_query($query);
        
        if($set){
            $result = mysql_fetch_array($set);
           
            if($result){
                
                return $result;
            }
        }
        
        return false;
        
    }

    
    public function delete($id){
        if($_SERVER["REQUEST_METHOD"]=='POST'){
            $result = $this->_model->delete($id);
            if($result !== false){
                header('Location: blog');
                exit;
            }
        }
    }

}